package cn.larry.tools.string;

/**
 * @version 1.1
 * @Author llin
 * @Date 2020/11/12 09:47
 */
public class StringTool {

    public StringTool() {
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    /**
     * 比较字符串格式的正整数型大小
     *
     * @param intStr1
     * @param intStr2
     * @return true：intStr1 > intStr2;
     * false：intStr1 ≤ intStr2
     */
    public static boolean compareTo(String intStr1, String intStr2) {
        if (intStr1.length() < intStr2.length()) {
            return false;
        } else if (intStr1.length() > intStr2.length()) {
            return true;
        } else {
            return intStr1.compareTo(intStr2) > 0 ? true : false;
        }
    }

    /**
     * 对 敏感 数据隐藏（隐藏数据长度取决于实际数据长度）
     *
     * @param str      需隐藏的敏感数据
     * @param start    需显示str之前的多少个字符
     * @param end      需显示str之后的多少个字符
     * @param placeStr 将敏感数据替换显示的字符
     * @return
     */
    public static String replaceMask(String str, int start, int end, String placeStr) {
        if (StringTool.isBlank(str)) {
            return "";
        }

        String indexStrategy = indexStrategy(str, start, end, placeStr);
        if (StringTool.isNotBlank(indexStrategy)) {
            return indexStrategy;
        }

        if (StringTool.isBlank(placeStr)) {
            placeStr = "*";
        }
        return str.replaceAll("(?<=[\\S]{" + start + "})\\S(?=[\\S]{" + end + "})", placeStr);
    }

    private static String indexStrategy(String str, int start, int end, String placeStr) {

        String respStr = "";
        if (start < 0 || end < 0) {
            respStr = str;
        } else if (start == 0 && end == 0) {
            if (StringTool.isNotBlank(placeStr))
                respStr = placeStr;
            else
                respStr = "*";
        }
        return respStr;
    }

}
